package com.edu.scau.dao.implement;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Component;

import com.edu.scau.dao.SoldDao;
import com.edu.scau.entity.Commodity;
import com.edu.scau.entity.OrderForm;
import com.edu.scau.entity.Sold;
@Component
public class SoldDaoImp implements SoldDao {
	@Resource(name="sessionFactory")
	private SessionFactory factory;

	public boolean addCommodity(OrderForm orderForm, Commodity commodity,
			int number) {
		// TODO Auto-generated method stub
		if (orderForm == null || commodity == null || number == 0)
			return false;
		Sold sold = new Sold();
		sold.setCommodity(commodity);
		sold.setOrderForm(orderForm);
		sold.setNumber(number);
		save(sold);
		return true;
	}

	@SuppressWarnings("unchecked")
	public boolean delCommodity(OrderForm orderForm, Commodity commodity) {
		// TODO Auto-generated method stub
		if (orderForm == null || commodity == null)
			return false;
		Session session = factory.getCurrentSession();
		Criteria criteria = session.createCriteria(Sold.class);
		List<Sold> list=new ArrayList<Sold>();
		
		 list= criteria
				.add(Restrictions.like("orderForm", orderForm))
				.add(Restrictions.like("commodity", commodity)).list();
		if (list.isEmpty()) {
			return true;
		}
		for(Sold so:list){
			delSold(so);
		}
		return true;
	}

	public boolean delSold(Sold sold) {
		// TODO Auto-generated method stub
		try {
			// TODO Auto-generated method stub
			Session session = factory.getCurrentSession();
			session.delete(sold);
		} catch (HibernateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
		return true;

	}

	public boolean save(Sold sold) {
		// TODO Auto-generated method stub
		try {
			// TODO Auto-generated method stub
			Session session = factory.getCurrentSession();
			session.save(sold);
		} catch (HibernateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
		return true;

	}

	public boolean update(Sold sold) {
		// TODO Auto-generated method stub
		try {
			// TODO Auto-generated method stub
			Session session = factory.getCurrentSession();
			session.update(sold);
		} catch (HibernateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
		return true;
	}

	public SessionFactory getFactory() {
		return factory;
	}

	public void setFactory(SessionFactory factory) {
		this.factory = factory;
	}

	@SuppressWarnings("unchecked")
	public List<Sold> queryAll() {
		// TODO Auto-generated method stub
		Session session = factory.getCurrentSession();
		Criteria criteria = session.createCriteria(Sold.class);
		List<Sold> list=new ArrayList<Sold>();
		
		 list= criteria.list();
		return list;
	}

}
